package cn.tedu.api;
/**
 * @Author 金帮嘉诚
 * @Date 2021-06-15 周二 20:24
 */
/*本类用于测试*/

import java.util.Objects;

/**
 * 知识点：
 * Object的equals（）和toString（）方法
 */
public class TestObject {
    public static void main(String[] args) {
        Student s1 = new Student(11,"11");
        Student s2 = new Student(11,"11");
        System.out.println("s1 = " + s1);
        //在重写toString之前，打印的是地址值，重写之后输出的是属性值
        /*toString()默认打印地址值，重写之后打印属性值*/
        System.out.println("s1.toString() = " + s1.toString());
        //hashCode输出哈希码
        System.out.println("s1.hashCode() = " + s1.hashCode());//460141958
        System.out.println("s2.hashCode() = " + s2.hashCode());//1163157884
        /*equals()默认比较地址值，重写之后比较的是对象的类型和所有属性值，若地址值相同也会返回true*/
        System.out.println("s1.equals(s2) = " + s1.equals(s2));
        System.out.println("(s2==s1) = " + (s2==s1));

    }
}

class Student {
    int id;
    String name;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id &&
                Objects.equals(name, student.name);
    }

//    @Override
//    public int hashCode() {
//        return Objects.hash(id, name);
//    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Student() {
    }

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
